/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.scs.util;

import com.wisnu.scs.dao.DbBarangDao;
import com.wisnu.scs.dao.DbBarangKeluarDao;
import com.wisnu.scs.dao.DbBarangMasukDao;
import com.wisnu.scs.dao.DbBarangRekapDao;
import com.wisnu.scs.dao.impl.DbBarangDaoImpl;
import com.wisnu.scs.dao.impl.DbBarangKeluarDaoImpl;
import com.wisnu.scs.dao.impl.DbBarangMasukDaoImpl;
import com.wisnu.scs.dao.impl.DbBarangRekapDaoImpl;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Hibernate Utility class with a convenient method to get Session Factory
 * object.
 *
 * @author Wisnu Wardoyo
 */
public class HibernateUtil {

    private static SessionFactory sessionFactory;

    private static DbBarangDao barangDao;

    private static DbBarangKeluarDao barangKeluarDao;

    private static DbBarangMasukDao barangMasukDao;

    private static DbBarangRekapDao barangRekapDao;

    protected static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            try {
                Properties properties = new Properties();
                properties.load(new FileInputStream("./service/Hibernate.properties"));
                Configuration configuration = new Configuration().configure();
                configuration.addProperties(properties);
                sessionFactory = configuration.buildSessionFactory();
            } catch (HibernateException ex) {
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        }
        return sessionFactory;
    }

    public static DbBarangDao getBarangDao() {
        if (barangDao == null) {
            barangDao = new DbBarangDaoImpl(getSessionFactory());
        }
        return barangDao;
    }

    public static DbBarangKeluarDao getBarangKeluarDao() {
        if (barangKeluarDao == null) {
            barangKeluarDao = new DbBarangKeluarDaoImpl(getSessionFactory());
        }
        return barangKeluarDao;
    }

    public static DbBarangMasukDao getBarangMasukDao() {
        if (barangMasukDao == null) {
            barangMasukDao = new DbBarangMasukDaoImpl(getSessionFactory());
        }
        return barangMasukDao;
    }

    public static DbBarangRekapDao getBarangRekapDao() {
        if (barangRekapDao == null) {
            barangRekapDao = new DbBarangRekapDaoImpl(getSessionFactory());
        }
        return barangRekapDao;
    }

}
